package com.ctl.jvm;

/**
 * <p>Title: InheritableThreadLocalTest</p>
 * <p>Description: 子线程获取父线程中 ThreadLocal 中的值</p>
 * <p>Copyright: Copyright (c) 2019</p>
 * <p>Company: www.hanshow.com</p>
 *
 * @author guolin
 * @version 1.1
 * @date 2022-04-06 10:00
 */
public class InheritableThreadLocalTest {
    public static void main(String[] args) throws InterruptedException {
        Thread parentParent = new Thread(() -> {
            ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
            threadLocal.set(1);
            InheritableThreadLocal<Integer> inheritableThreadLocal = new InheritableThreadLocal<>();
            inheritableThreadLocal.set(2);
            System.out.println(inheritableThreadLocal.get());
            new Thread(() -> {
                System.out.println("threadLocal=" + threadLocal.get());
                System.out.println("inheritableThreadLocal=" + inheritableThreadLocal.get());
            }).start();
        }, "父线程");
        parentParent.start();
    }
}
